using System;
using System.Collections.Generic;
using System.Text;

using NLog;
using NLog.Targets;
using NLog.Targets.Wrappers;

using System.Windows.Forms;

using System.Threading;

using Microsoft.Win32;

namespace Parsifal
{
    public class Utility
    {
        
        private Logger logger = LogManager.GetCurrentClassLogger(); 
    
        public Utility()
        {
            FileTarget target = new FileTarget();
            target.Layout = "${date} ${level} ${message}";
            target.FileName = "${basedir}/logs/logfile.txt";
            target.KeepFileOpen = false;
            target.Encoding = "iso-8859-2";
            target.MaxArchiveFiles = 50000;
            //target.DeleteOldFileOnStartup = true;
            //target.ReplaceFileContentsOnEachWrite = true;








            AsyncTargetWrapper wrapper = new AsyncTargetWrapper();
            wrapper.WrappedTarget = target;
            wrapper.QueueLimit = 5000;
            wrapper.OverflowAction = AsyncTargetWrapperOverflowAction.Discard;

            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(wrapper, LogLevel.Info);
            logger.Info("                                                                                         ");
            logger.Info("----------------------------------------------AVVIO APPLICAZIONE------------------------");
            logger.Info("                                               ok                                          ");
            
        }


        public void LogEvent(string data,LogLevel eLevel)
        {
            Logger logger = LogManager.GetLogger("1");
            logger.Log(eLevel, data);
        }

        public void LogEvent(string data)
        {
            Logger logger = LogManager.GetLogger("1");
            logger.Debug(data);
        }

        public byte[] ConvertStringToBye(string data)
        {

            byte[] dataRes = new byte[1024];
            dataRes = Encoding.UTF8.GetBytes(data);

            return dataRes;
        }

        


        public void WaitFor(int millisecond)
        {
            Thread.Sleep(millisecond);

        }

        public string[] OpenFileDialogBox()
        {
            try
            {
                //OpenFileDialog dlgOpenFile = new OpenFileDialog();
                //dlgOpenFile.InitializeLifetimeService();

                //dlgOpenFile.Filter = "Mp3 (*.mp3)|*.mp3|Media Audio (*.wma)|*.wma|Media Video (*.wmv)|*.wmv|All files (*.*)|*.*";

                //dlgOpenFile.Multiselect = true;

                OpenFileDialog opd = new OpenFileDialog();
                opd.InitializeLifetimeService();

                opd.Title = "My Open File Dialog Example via C#";
                opd.DefaultExt = ".txt";

                opd.Filter = "Text File|*.txt|All File|(*.*)";




                if (opd.ShowDialog() == DialogResult.OK)
                {

                    // If ReadOnlyChecked is true, uses the OpenFile method to
                    // open the file with read/only access.
                    return opd.FileNames;

                }
                else
                {
                    string[] ret = { "" };
                    return ret;
                }

                }
                catch (Exception err)
                {
                    this.LogEvent("Utility :: Errore nell'apertura del file selezionato ", LogLevel.Error);

                    throw new Exception("Errore nell'apertura del file selezionato...");
                    
                }

                return null;
        }

        public void SetRegistryLimitForMediaEncoder()
        {
            

            RegistryKey rk = Registry.ClassesRoot;
            RegistryKey regKey = rk.OpenSubKey("Software\\Microsoft\\Windows Media Tools\\Encoder\\MaxClientConnections");
            Registry.SetValue("HKEY_CLASSES_ROOT\\Software\\Microsoft\\Windows Media Tools\\Encoder\\MaxClientConnections","", 15,RegistryValueKind.String); 
            //regKey.Name;
        }

    }
}
